package lowijs.XfDroid;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.*;

import lowijs.XfireClient.*;
import lowijs.XfireClient.event.*;

public class Login extends Activity implements XfireObserver {
	private EditText etUsername = null;
	private EditText etPassword = null;
	private Button btLogin = null;

	private ProgressDialog pd = null;
	
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.login);
	
		etUsername = (EditText)findViewById(R.id.etUsername);
		etPassword = (EditText)findViewById(R.id.etPassword);
		btLogin = (Button)findViewById(R.id.btLogin);
		
		OnClickListener loginListener = new OnClickListener() {
			public void onClick(View v) {
				showProgressDialog();
				loginToXfire(etUsername.getText().toString(),
						etPassword.getText().toString());
			}
		};
		btLogin.setOnClickListener(loginListener);
		
		Intent i = getIntent();
		if (i != null && i.getBooleanExtra("dcReason", false))
			toastHandler.sendEmptyMessage(R.string.logged_in_elsewhere);
	}
	
	public void onStart() {
		super.onStart();
		EventManager.addObserver(this);
		
		SharedPreferences pr = getPreferences(MODE_PRIVATE);
		etUsername.setText(pr.getString("username", "Username"));
		etPassword.setText(pr.getString("password", "Password"));
	}
	
	public void onStop() {
		super.onStop();
		EventManager.removeObserver(this);

		SharedPreferences.Editor pr = getPreferences(MODE_PRIVATE).edit();
		pr.putString("username", etUsername.getText().toString());
		pr.putString("password", etPassword.getText().toString());
		pr.commit();
		
		if (pd != null)
			pd.dismiss();
	}

	public void onSaveInstanceState(Bundle b) {
		b.putBoolean("pd", pd != null && pd.isShowing());
		super.onSaveInstanceState(b);
	}
	
	public void onRestoreInstanceState(Bundle b) {
		if (b.getBoolean("pd")) {
			showProgressDialog();
			b.remove("pd");
		}
		super.onRestoreInstanceState(b);
	}
	
	private void loginToXfire(String username, String password) {
		Intent i = new Intent(this, XfDroidService.class);
		i.putExtra("username", username);
		i.putExtra("password", password);
		startService(i);
	}
	
	private void showProgressDialog() {
		pd = new ProgressDialog(this);
		pd.setTitle(R.string.please_wait);		
		pd.setMessage(getString(R.string.logging_in));
		pd.setCancelable(false);
		pd.show();
	}
	
	private Handler toastHandler = new Handler() {
		public void handleMessage(Message m) {
			Toast t = Toast.makeText(getBaseContext(), m.what, 2);
			t.show();
		}
	};
	
	public void handleEvent(XfireEvent event) {
		switch (event.getEventType()) {
		case XfireEvent.XF_FLRECV:
			pd.dismiss();
			Intent i = new Intent(this, Friendslist.class);
			i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(i);
			finish();
			return;
		case XfireEvent.XF_DCREASON:
		case XfireEvent.XF_LOGINFAIL:
			pd.dismiss();
			toastHandler.sendEmptyMessage(R.string.login_failed);
			return;
		}
	}
}
